<?php
namespace Home\Controller;
use Think\Controller;

/**
 * Person Related Display Controller
 *
 * @package Home\Controller
 * @author lythesia
**/
class PersonController extends Controller {
  /**
   * Display person index page(single person)
   *
   * @param  string person_id(`$_GET[]`)
   * @return 
   * @author lythesia
   **/
  public function index($id) {
    $opt = array(
      'rel' => ['movies', 'genres'],
    );

    $field = 'id,name,aka,birthdate,deathdate,occupation,resume,poster,imdb_field,dbpedia_field';

    $data = A('Common/Person')->getPersonRelById($id, $opt, $field);
    if(!$data) goto error;

    $data['imdb_field'] = explode(',', $data['imdb_field']);
    $data['dbpedia_field'] = explode(',', $data['dbpedia_field']);

    list($_birth, $_death) = array(
      $data['birthdate'] ? explode('-', $data['birthdate'])[0] : '',
      $data['deathdate'] ? explode('-', $data['deathdate'])[0] : '',
    );
    $_cap = $data['name'];
    if($_birth or $_death) $_cap .= '( '.$_birth.' - '.$_death.' )';

    /* generate timeline data */
    $timeline = array(
      'headline'  => $data['name'].' - Filmography snapshot',
      'type'      => 'default',
      'text'      => '<p>'.implode(' - ', explode(',', $data['occupation'])).'</p>',
      'asset'     => array(
        'media'   => $data['poster'],
        'caption' => $_cap,
      ),
    );
    foreach($data['movies'] as $val) {
      $timeline['date'][] = array(
        'startDate'   => str_replace('-', ',', $val['releasedate']),
        'headline'    => $val['title'],
        'text'        => '<p>'.($val['summary'][count($val['summary'])-1] == '.' ? $val['summary'] : $val['summary'].'.').'</p>',
        'tag'         => explode(',', $val['genre'])[0],
        'asset'       => array(
          'media'     => $val['poster'],
        ),
      );
    }
    $timeline_data = json_encode(array('timeline' => $timeline));

    /* generate radarchart data*/
    $radar = array(
      'movie_num'   => 0,
      'rating_avg'  => 0.0,
      'ratingnum'   => 0,
      'co-workers'  => 0,
      'active'      => 0,
      'diversity'   => 0,
    );
    // movie #
    $radar['movie_num'] = count($data['movies']);
    // avg
    $radar['rating_avg'] = 0;
    $_cnt = 0;
    foreach($data['movies'] as $val) {
      if($val) {
        $radar['rating_avg'] += intval($val['rating']);
        $_cnt++;
      }
    }
    $radar['rating_avg'] /= $_cnt;
    // rating #
    $radar['ratingnum'] = 0;
    foreach($data['movies'] as $val) {
      if($val) {
        $radar['ratingnum'] += intval($val['ratingnum']);
      }
    }
    // co-workers
    $movie_ids = array();
    foreach($data['movies'] as $val) {
      $movie_ids[] = $val['id'];
    }
    $radar['co-workers'] = M('personmovie')->where(array('object' => array('IN', $movie_ids)))->count();
    $radar['co-workers'] = intval($radar['co-workers']) - $radar['movies'];
    // active
    foreach($data['movies'] as $val) {
      if($val && $val['releasedate']) {
        if(strtotime($val['releasedate']) > mktime(0, 0, 0, date('m'), date('d'), date('Y')-5)) {
          $radar['active']++;
        }
      }
    }
    // diversity
    $radar['diversity'] = count($data['genres']);
    // summarize
    $radar['movie_num']  /= C('radar_limit.movie_num');
    $radar['rating_avg'] /= C('radar_limit.rating_avg');
    $radar['ratingnum']  = log($radar['ratingnum'])/log(C('radar_limit.ratingnum'));
    $radar['co-workers'] /= C('radar_limit.co-workers');
    $radar['active']     /= C('radar_limit.active');
    $radar['diversity']  /= C('radar_limit.diversity');
    $radar_data = json_encode(array(
      array(
        'type'  => 'Movies',
        'values'=> min(0.99, number_format($radar['movie_num'], 2))
      ),
      array(
        'type'  => 'Rating',
        'values'=> min(0.99, number_format($radar['rating_avg'], 2))
      ),
      array(
        'type'  => 'Popularity',
        'values'=> min(0.99, number_format($radar['ratingnum'], 2))
      ),
      array(
        'type'  => 'Sociability',
        'values'=> min(0.99, number_format($radar['co-workers'], 2))
      ),
      array(
        'type'  => 'Activeness',
        'values'=> min(0.99, number_format($radar['active'], 2))
      ),
      array(
        'type'  => 'Diversity',
        'values'=> min(0.99, number_format($radar['diversity'], 2))
      ),
    ));

    $this->assign('data', $data);
    $this->assign('timeline_data', $timeline_data);
    $this->assign('radar_data', $radar_data);
    $this->assign('script', 'Common/script-tm');

    $this->display('Person/single-person');
    return;
 
    error:
    $this->display('Common/404');
  }
}
